package oneweone.com.readerviewdemo;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.tencent.smtt.sdk.TbsReaderView;

import java.io.File;

/**
 * @author jzy
 * created at 2019/1/10
 * desc
 */
public class ReaderView extends FrameLayout implements TbsReaderView.ReaderCallback {

    private final String TAG = getClass().getSimpleName();

    private TbsReaderView mTbsReaderView;

    public ReaderView(@NonNull Context context) {
        this(context, null);
    }

    public ReaderView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ReaderView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mTbsReaderView = new TbsReaderView(getContext(), this);
        this.addView(mTbsReaderView, new LinearLayout.LayoutParams(-1, -1));
    }

    @Override
    public void onCallBackAction(Integer integer, Object o, Object o1) {
        LogUtils.e("****************************************************" + integer);
    }

    public void onStopDisplay() {
        if (mTbsReaderView != null) {
            mTbsReaderView.onStop();
        }
    }


    private TbsReaderView getTbsReaderView(Context context) {
        return new TbsReaderView(context, this);
    }

    public void displayFile(File mFile) {

        if (mFile != null && !TextUtils.isEmpty(mFile.toString())) {
            //增加下面一句解决没有TbsReaderTemp文件夹存在导致加载文件失败
            String bsReaderTemp = getContext().getExternalFilesDir("").getAbsolutePath();
            File bsReaderTempFile = new File(bsReaderTemp);

            if (!bsReaderTempFile.exists()) {
                bsReaderTempFile.mkdirs();
            }

            //加载文件
            Bundle localBundle = new Bundle();
            LogUtils.d(mFile.toString());
            localBundle.putString("filePath", mFile.toString());
            localBundle.putString("tempPath", bsReaderTemp);

            if (this.mTbsReaderView == null)
                this.mTbsReaderView = getTbsReaderView(getContext());
            boolean bool = this.mTbsReaderView.preOpen(getFileType(mFile.toString()), false);
            if (bool) {
                this.mTbsReaderView.openFile(localBundle);
            }
        } else {
            LogUtils.e("文件路径无效！");
        }

    }

    /***
     * 获取文件类型
     *
     * @param paramString
     * @return
     */
    private String getFileType(String paramString) {
        String str = "";

        if (TextUtils.isEmpty(paramString)) {
            LogUtils.d(TAG, "路径无效---->null");
            return str;
        }
        LogUtils.d(TAG, "路径:" + paramString);
        int i = paramString.lastIndexOf('.');
        if (i <= -1) {
            LogUtils.d(TAG, "i <= -1");
            return str;
        }


        str = paramString.substring(i + 1);
        LogUtils.d(TAG, "文件类型------>" + str);
        return str;
    }
}
